package com.wangyadong.hobby.test.test1;

import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPoolExecutor {
    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
    private static final int COUNT_BITS = Integer.SIZE - 3;
    private static final int CAPACITY = (1 << COUNT_BITS) - 1;

    // runState is stored in the high-order bits
    private static final int RUNNING = -1 << COUNT_BITS;
    private static final int SHUTDOWN = 0 << COUNT_BITS;
    private static final int STOP = 1 << COUNT_BITS;
    private static final int TIDYING = 2 << COUNT_BITS;
    private static final int TERMINATED = 3 << COUNT_BITS;

    // Packing and unpacking ctl
    private static int runStateOf(int c) {
        String binaryString = Integer.toBinaryString(c);
        System.out.println("value c  " + c + " to binary is " + binaryString);
        return c & ~CAPACITY;
    }

    private static int workerCountOf(int c) {
        return c & CAPACITY;
    }

    private static int ctlOf(int rs, int wc) {
        return rs | wc;
    }

    public static void main(String[] args) {
        System.out.println(COUNT_BITS);
        System.out.println(Integer.SIZE - 3);
        System.out.println(RUNNING);
        System.out.println(SHUTDOWN);
        System.out.println(STOP);
        System.out.println(TIDYING);
        System.out.println(TERMINATED);
        System.out.println(CAPACITY);
        System.out.println(-536870912 - 1);
        boolean b = runStateOf(SHUTDOWN) == SHUTDOWN;
        System.out.println(b);

        System.out.println("========================================================");
        String s = Integer.toBinaryString((1 << COUNT_BITS));
        System.out.println(s);
        s = Integer.toBinaryString((1 << COUNT_BITS) - 1);
        System.out.println(s);
        s = Integer.toBinaryString(RUNNING);
        System.out.println(s);
        System.out.println(Integer.toBinaryString(CAPACITY));
        System.out.println(Integer.toBinaryString(runStateOf(3)));
        String s1 = Integer.toBinaryString(~1);
        System.out.println(s1);
        System.out.println(workerCountOf(3));
        System.out.println(runStateOf(3));

    }
}
